import * as api from '@/services/PriceMgr';
import _ from 'lodash';

const hasChildren = (list, item) => {
  let result = false;
  list.forEach(value => {
    if (value.pid === item.id) {
      result = true;
    }
  });
  return result;
};

const getTargetVersionIds = list => {
  const stageIds = [];
  _.each(list.data, item => {
    stageIds.push({
      stageIds: item.stageId,
      stageName: item.stageName,
      versionId: item.itemInfo[0].versionId,
    });
  });
  const newTargetVersionIds = [
    {
      versionType: list.versionType,
      versionName: list.versionName,
      stageIds,
    },
  ];
  return newTargetVersionIds;
};

const getContrastVersionIds = (list, key, checked) => {
  const cloneList = _.cloneDeep(list);
  _.each(cloneList, v => {
    const x = v;
    if (v.versionType === key) {
      x.checked = checked === 'null' ? v.checked : checked;
    }
  });
  return cloneList;
};

const getExpandedRowKeys = list => {
  const expandedRowKeys = [];
  _.forEach(list, value => {
    expandedRowKeys.push(value.id);
  });
  return expandedRowKeys;
};

const buildTree = (list, pid = '0') => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    if (x.pid === pid) {
      if (hasChildren(list, x)) {
        x.children = [];
        x.children = buildTree(list, x.id);
      }
      tree.push(x);
    }
  });
  return tree;
};

const Model = {
  namespace: 'VersionComparison',
  state: {
    targetVersionIds: [], // 目标版本id
    contrastiveVersionIds: [], // 对比版本id
    versionItem: [
      {
        versionType: 'newest',
        versionName: '最新全盘',
        data: [],
      },
      {
        versionType: 'dynamic',
        versionName: '动态版',
        data: [],
      },
      {
        versionType: 'pricing',
        versionName: '大定价版',
        data: [],
      },
      {
        versionType: 'strategy',
        versionName: '战规版',
        data: [],
      },
      {
        versionType: 'investment',
        versionName: '投资版',
        data: [],
      },
    ], // 版本信息数据
    contrastOliveItem: [],
    expandedRowKeys: [],
    contrastDetailItem: [],
    loading: true,
  },
  effects: {
    //  请求版本信息
    *versionInfo({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'save',
        payload: {
          loading: true,
        },
      });
      const { projectId, versionType } = payload.params;
      const params = {
        projectId,
        versionType: [versionType === 'newest' ? '' : versionType],
      };
      const versionItem = yield select(state => state.VersionComparison.versionItem);
      const response = yield call(api.priceVersionCompareBasicInfoUsingGET, { params });
      if (response && response.code === 1) {
        _.each(versionItem, value => {
          const x = value;
          if (x.versionType === versionType) {
            x.data = response.data;
          }
        });
        yield put({
          type: 'save',
          payload: {
            versionItem,
          },
        });
        if (versionType === 'newest') {
          const targetItems = _.filter(versionItem, v => v.versionType === 'newest')[0];
          const targetVersionIds = getTargetVersionIds(targetItems);
          yield put({
            type: 'save',
            payload: {
              targetVersionIds,
            },
          });
        }
        if (versionType !== 'newest') {
          const contrastiveVersionIds = [];
          _.each(versionItem, element => {
            if (element.versionType !== 'newest') {
              const stageIds = [];
              _.each(element.data, v => {
                stageIds.push({
                  stageIds: v.stageId,
                  stageName: v.stageName,
                  versionId: v.itemInfo[0].versionId,
                });
              });
              contrastiveVersionIds.push({
                versionType: element.versionType,
                versionName: element.versionName,
                checked: false,
                stageIds,
              });
            }
          });
          yield put({
            type: 'save',
            payload: {
              contrastiveVersionIds,
            },
          });
        }
        if (versionType === 'investment') {
          yield put({
            type: 'save',
            payload: {
              loading: false,
            },
          });
        }
      }
    },
    // 对比概览
    *getContrastOlive({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          contrastOliveItem: [],
        },
      });
      const params = {
        code: payload.code,
      };
      const { data } = payload;

      const response = yield call(api.valueDifferenceReviewUsingPOST, { params, data });
      if (response && response.code === 1) {
        yield put({
          type: 'save',
          payload: {
            contrastOliveItem: response.data,
          },
        });
      }
    },
    // 对比详情
    *getContrastDetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          expandedRowKeys: [],
          contrastDetailItem: [],
        },
      });
      const response = yield call(api.priceVersionCompareUsingPOST, payload);
      if (response && response.code === 1) {
        const { data } = response;
        const expandedRowKeys = getExpandedRowKeys(data);
        const treeData = buildTree(data);
        yield put({
          type: 'save',
          payload: {
            expandedRowKeys,
            contrastDetailItem: treeData,
          },
        });
      }
    },
    *editSelect({ payload }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          [payload.key]: payload.item,
        },
      });
    },
    *setTarget({ payload }, { call, put }) {
      const targetVersionIds = getTargetVersionIds(payload.data);
      yield put({
        type: 'save',
        payload: {
          targetVersionIds,
        },
      });
    },
    *setContrast({ payload }, { call, put, select }) {
      const all = yield select(state => state.VersionComparison.contrastiveVersionIds);
      const contrastiveVersionIds = getContrastVersionIds(all, payload.key, payload.checked);
      yield put({
        type: 'save',
        payload: {
          contrastiveVersionIds,
        },
      });
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
